package com.vcc.boss.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckSafe;
import com.mybatisflex.core.paginate.Page;
import com.vcc.core.annotations.RestPack;
import com.vcc.core.exception.NotFoundException;
import com.vcc.service.boss.request.kyc.AuditKycDto;
import com.vcc.service.boss.response.kyc.KycCertInfoVoBoss;
import com.vcc.service.client.vo.kyc.kyb.EnterpriseCertInfoVo;
import com.vcc.service.sa.BossUserUtil;
import com.vcc.service.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("kyc")
public class KycController {

    @Autowired
    private KycCertInfoService kycCertInfoService;
    @Autowired
    private KycSuccessCertInfoService kycSuccessCertInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private BossUserService bossUserService;
    @Autowired
    private EnterpriseCertInfoService enterpriseCertInfoService;

    @SaCheckPermission(value = "client:kyc:list:view")
    @RestPack
    @SaCheckLogin
    @GetMapping("paging")
    public Page<KycCertInfoVoBoss> paging(Integer pageNumber, Integer pageSize, Integer status, String certName, Long targetUserId) {
        if (targetUserId != null && targetUserId != 0L) {
            Long dealerId = userService.getDealerIdById(targetUserId);
            if (!BossUserUtil.hasAllData() && !dealerId.equals(BossUserUtil.getId())) {
                throw new NotFoundException("无权查看该用户的KYC信息");
            }
        }
        Page<KycCertInfoVoBoss> paging = kycCertInfoService.paging(
                Page.of(pageNumber != null ? pageNumber : 1, pageSize != null ? pageSize : 20)
                , status, certName, targetUserId);
        return paging.map(kycCertInfo -> {
            String auditUserName = "";
            if (kycCertInfo.getAuditUserId() != null) {
                kycCertInfo.setAuditAccount(bossUserService.getUserNameById(kycCertInfo.getAuditUserId()));
            }
            return kycCertInfo;
        });
    }

    @SaCheckPermission(value = "client:kyb:list:view")
    @RestPack
    @SaCheckLogin
    @GetMapping("kybPaging")
    public Page<EnterpriseCertInfoVo> kybPaging(Integer pageNumber, Integer pageSize, Integer status, String certName, Long targetUserId) {
        if (targetUserId != null && targetUserId != 0L) {
            Long dealerId = userService.getDealerIdById(targetUserId);
            if (!BossUserUtil.hasAllData() && !dealerId.equals(BossUserUtil.getId())) {
                throw new NotFoundException("无权查看该用户的KYC信息");
            }
        }
        Page<EnterpriseCertInfoVo> paging = enterpriseCertInfoService.paging(
                Page.of(pageNumber != null ? pageNumber : 1, pageSize != null ? pageSize : 20)
                , status, certName, targetUserId);
        return paging.map(it -> {
            if (it.getAuditUserId() != null) {
                it.setAuditAccount(bossUserService.getUserNameById(it.getAuditUserId()));
            }
            return it;
        });
    }


    @SaCheckPermission(value = "client:kyc:list:audit")
    @RestPack
    @SaCheckLogin
    @PostMapping("audit")
    @SaCheckSafe("CLIENT_KYC_AUDIT")
    public Boolean audit(@RequestBody @Validated AuditKycDto auditKycDto) {
        return kycCertInfoService.audit(auditKycDto);
    }

    @SaCheckPermission(value = "client:kyb:list:audit")
    @RestPack
    @SaCheckLogin
    @PostMapping("auditKyb")
    @SaCheckSafe("CLIENT_KYB_AUDIT")
    public Boolean auditKyb(@RequestBody @Validated AuditKycDto auditKycDto) {
        return enterpriseCertInfoService.audit(auditKycDto);
    }

}
